package com.deep.product.controller;

import java.util.List;
import java.util.Map;

import com.deep.product.model.entity.ProductAttrValueEntity;
import com.deep.product.model.vo.AttrRespVO;
import com.deep.product.model.vo.AttrVO;
import com.deep.product.service.AttrService;
import com.deep.product.service.ProductAttrValueService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.deep.common.utils.PageUtils;
import com.deep.common.utils.R;


/**
 * 商品属性规格
 *
 * @author deep
 * @email ${email}
 * @date 2022-01-13 17:09:54
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 更新规格属性
     *
     * @param spuId
     * @param entities
     * @return
     */
    @PostMapping("/update/{spuId}")
    public R updateSpiAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities) {
        productAttrValueService.updateSpuAttr(spuId, entities);
        return R.ok();
    }

    /**
     * 获取属性规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> entities = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询属性详情（进行回显）
     */
    @RequestMapping("/info/{attrId}")
    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVO respVO = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", respVO);
    }

    /**
     * 保存属性
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVO attrVO) {
        attrService.saveAttrVO(attrVO);

        return R.ok();
    }

    /**
     * 修改属性
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVO attrVO) {
        attrService.updateAttrVO(attrVO);

        return R.ok();
    }

    /**
     * 获取基本/销售属性
     *
     * @param type 属性类型[0-销售属性，1-基本属性，2-既是销售属性又是基本属性]
     * @return 包含属性分类的页面
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType") String type) {

        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, type);
        return R.ok().put("page", page);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
//        attrService.removeByIds(Arrays.asList(attrIds));

        attrService.deleteDetail(attrIds);

        return R.ok();
    }

}
